﻿using AutoMapper;
using B.S.ProductionProject.Domain;
using B.S.ProductionProject.ErrorCode;
using B.S.ProductionProject.Write.WebAPI.Applications.Commands.ProductionScheduleDetailCommand;
using MediatR;
using SqlSugar;

namespace B.S.ProductionProject.Write.WebAPI.Applications.CommandHandlers.ProductionScheduleDetailCommandHandler
{
    public class AddProductionScheduleDetailCommandHandler : IRequestHandler<AddProductionScheduleDetailCommand, APIResult<object>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper _mapper;

        public AddProductionScheduleDetailCommandHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            _mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(AddProductionScheduleDetailCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.code = APIResultCode.success;
            result.msg = "添加明细成功";

            var model = _mapper.Map<ProductionScheduleDetail>(request);
            await db.Insertable(model).ExecuteCommandAsync();

            return await Task.FromResult(result);
        }
    }
}
